<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">主页</el-breadcrumb-item>
            <el-breadcrumb-item><a href="/">系统设置</a></el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片 -->
        <el-card class="box-card">
            <!-- 栅格 -->
            <el-row :gutter="20">
                <el-col :span="14">
                    <el-button type="primary" @click="clickSettingfun">添加配置项</el-button>
                </el-col>
                <el-col :span="10">
                    <el-input placeholder="关键词" v-model="searchdata" class="input-with-select" clearable>
                        <el-button slot="append" icon="el-icon-search"></el-button>
                    </el-input>
                </el-col>
            </el-row>
            <!-- 表格 -->
            <el-table class="tabletop" :border="true" :data="SettingtableData" style="width: 100%">
                <el-table-column prop="name" label="名称">
                </el-table-column>
                <el-table-column prop="code" label="编码">
                </el-table-column>
                <el-table-column prop="values" label="值">
                </el-table-column>
                <el-table-column label="创建时间">
                    <template slot-scope="scope">
                        {{ scope.row.ctime | dataFilter }}
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="editSettingfun(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="delSettingfun(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <!-- 弹出的对话框 -->
        <el-dialog :code="truefalse == false ? '添加视频列表' : '修改视频列表'" :before-close="Closethewindow"
            :visible.sync="dialogVisible" width="50%">
            <!-- 表单 -->
            <el-form :model="dialogBox" ref="dialogBoxrules" label-width="100px"
                class="demo-ruleForm">
                <el-form-item label="配置名称">
                    <el-input v-model="dialogBox.code"></el-input>
                </el-form-item>
                <el-form-item label="配置方法">
                    <el-input v-model="dialogBox.name"></el-input>
                </el-form-item>
                <el-form-item label="配置值">
                    <el-input type="textarea" :rows="2" placeholder="请输入配置值，用，隔开" v-model="dialogBox.values">
                    </el-input>
                </el-form-item>


            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="Closethewindow">取 消</el-button>
                <el-button type="primary" @click="addSettingfun">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
import { GetSettingList, AddSetting, EditSetting, DelSetting } from '../api/Setting.js'
export default {
    data() {
        return {
            // 数据存储
            SettingtableData: [],
            // 搜索关键词
            searchdata: '',
            // 对话框的显示和隐藏
            dialogVisible: false,
            // 对话框里绑定的数据
            dialogBox: {
                code: '',
                name: '',
                values: '',
            },
            // 控制添加和编辑的切换
            truefalse: false,
            // 编辑数据存放
            editdata: {}
        }
    },
    created() {
        this.getSetting()
    },
    methods: {
        // 点击添加按钮和编辑事件
        clickSettingfun() {
            this.truefalse = false
            this.dialogVisible = true
        },
        // 编辑对话框
        editSettingfun(row) {
            console.log(row);
            this.truefalse = true
            this.dialogVisible = true
            this.dialogBox = {
                code: row.code,
                name: row.name,
                values: row.values,
            }
            this.editdata = this.dialogBox
            this.editdata.id = row.id
        },
        // 点击确定按钮
        addSettingfun() {
            if (this.truefalse == false) {
                // 请求添加接口
                this.addSetting()
            } else {
                this.editSetting()
            }
            // 触发关闭对话框
            this.Closethewindow()
            // 刷新数据列表g
            this.getSetting()
        },

        // 删除
        async delSettingfun(row) {
            const confirm = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).catch(err => err)
            if (confirm !== 'confirm') return this.$message.info('已取消')
            this.delSetting(row.id)
        },
        // 关闭对话框事件
        Closethewindow() {
            this.dialogVisible = false
            this.dialogBox.code = ''
            this.dialogBox.name = ''
            this.dialogBox.values = ''
            this.editdata = {}
        },
        // 请求区😎😎😎😎😎
        // 列表数据渲染
        async getSetting() {
            const { data } = await GetSettingList()
            console.log(data);
            if (data.code !== 1) return this.$message.error('获取视频管理列表失败')
            // 数据赋值
            this.SettingtableData = data.list
            // 总条数赋值
            this.total = data.pager.total
        },
        // 添加
        async addSetting() {
            const { data } = await AddSetting(this.dialogBox)
            if (data.code !== 1) return this.$message.error('添加失败')
            this.$message.success('添加成功')
        },
        // 修改
        async editSetting() {
            const { data } = await EditSetting(this.editdata)
            console.log(data);
            if (data.code !== 1) return this.$message.error('修改失败')
            this.$message.success('修改成功')
        },
        // 删除
        async delSetting(id) {
            const { data } = await DelSetting({ id })
            if (data.code !== 1) return this.$message.error('删除失败')
            this.$message.success('删除成功')
            this.getSetting()
        }
    }
}
</script>

<style scoped>

</style>